<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-calendar"></i> 账号管理
                </el-breadcrumb-item>
                <el-breadcrumb-item>修改密码</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="form-box">
                <el-form ref="formRef" :rules="rules" :model="form" label-width="80px">
                    <el-form-item label="当前账号" prop="adminIdentify">
                        <el-input v-model="form.adminIdentify" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="旧密码" prop="adminPassword">
                        <el-input v-model="form.adminPassword" type="password"></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" prop="newPassword">
                        <el-input v-model="form.newPassword" type="password"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="pwd">
                        <el-input v-model="form.pwd" type="password"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit">修改密码</el-button>
                        <el-button @click="onReset">清空表单</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
import { reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import { modifyAdmin } from "../../api";
import { useRouter } from "vue-router";
export default {
    name: "adminUpdateForm",
    setup() {
        const rules = {
            adminIdentify: [
                { required: true, message: "请输入账号", trigger: "blur" },
            ],
            adminName: [
                { required: true, message: "请输入姓名", trigger: "blur" },
            ],
            adminPassword: [
                { required: true, message: "请输入密码", trigger: "blur" },
            ],
            pwd: [
                { required: true, message: "请输入确认密码", trigger: "blur" },
            ],
            newPassword: [
                { required: true, message: "请输入新密码", trigger: "blur" },
            ]
        };
        const formRef = ref(null);

        const adminIdentify = localStorage.getItem("ms_username");
        const adminId = localStorage.getItem("adminId");
        const adminName = localStorage.getItem("adminName");
        const adminPassword = localStorage.getItem("adminPassword");

        // 表单数据
        let form = reactive({
            newPassword: '',
            adminIdentify: adminIdentify,
            adminName: adminName,
            adminPassword: '',
            pwd: '',
            adminId: adminId
        });

        // 重置
        const onReset = () => {
            formRef.value.resetFields();
        };

        const router = useRouter();
        // 提交
        const onSubmit = () => {
            // 表单校验
            formRef.value.validate((valid) => {
                if (valid) {
                    if (form.pwd !== form.newPassword) {
                        ElMessage.error('新密码与确认密码不一致');
                    } else if (form.adminPassword !== adminPassword) {
                        ElMessage.error('旧密码不正确');
                    } else {
                        form.adminPassword = form.pwd;
                        console.log(form);
                        modifyAdmin(form).then(res => {
                            if (res.code === 200) {
                                onReset();
                                localStorage.clear();
                                router.push("/login");
                                ElMessage.success('修改成功, 请重新登录');
                            } else {
                                console.log("[修改密码] 失败: " + res.message);
                                ElMessage.error('服务器繁忙，请稍后重试');
                            }
                        })
                    }
                } else {
                    return false;
                }
            });
        };

        return {
            rules,
            formRef,
            form,
            onSubmit,
            onReset,
        };
    },
};
</script>